const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer();
const io = new Server(httpServer, {  
    cors: {                 //解决跨域问题
        origin: "*",
        methods: ["GET", "POST"]
  } 
});
var userbaindid = new Map()

io.on("connection", (socket) => {
  //保存当前客户端id
  //开始绑定两个用户 
  socket.on("bainduserid",(userid)=>{
      console.log(userid , socket.id);
      userbaindid.set(socket.id,userid)
      console.log(userbaindid,'查看数据结构');
    
  })  
  socket.on('sendMsg',(data) => {
      console.log(data.wsid)
      console.log(userbaindid);
    //   定义发送消息事件
    // 给对应的 用户 发送消息
    socket.to('AKfxSre24TZidSaAAAAF').emit("msg",data)


    
  })
  socket.on("zzyclear",(socketid)=>{
    console.log(socketid ,'聊天界面关闭');
    //解除绑定对象 传过来的是 当前绑定的 用户id
    if( userbaindid.has(socketid)) {
      userbaindid.delete(socketid)
    }
  })
});

httpServer.listen(3000,function(){
    console.log('http://127.0.0.1:3000')
});
